/*
 * Copyright (c) 2018-2999 广州亚米信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.api.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yami.shop.bean.model.User;
import com.yami.shop.bean.model.VipGiftRecord;
import com.yami.shop.bean.model.VipPackage;
import com.yami.shop.bean.param.VipBuyParam;
import com.yami.shop.dao.UserMapper;
import com.yami.shop.dao.VipPackageMapper;
import com.yami.shop.security.util.SecurityUtils;
import com.yami.shop.service.UserService;
import com.yami.shop.service.VipGiftRecordService;
import com.yami.shop.service.VipPackageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 赠送vip
 * @author lgh on 2018/10/26.
 */
@RestController
@RequestMapping("/p/vipgift")
@Api(tags = "赠送vip接口")
public class VipGiftController {

    @Autowired
    private VipGiftRecordService vipGiftRecordService;


    @Autowired
    private VipPackageService vipPackageService;

    @Autowired
    private UserService userService;


    @GetMapping("/available")
    @ApiOperation(value = "查看待领取的vip列表", notes = "")
    public ResponseEntity<List<VipGiftRecord>> available() {
        String userId = SecurityUtils.getUser().getUserId();

        List<VipGiftRecord> list = vipGiftRecordService.list(new LambdaQueryWrapper<>(new VipGiftRecord())
                .eq(VipGiftRecord::getUserId, userId)
                .eq(VipGiftRecord::getStatus, 0)
                .orderByDesc(VipGiftRecord::getCreateTime));
        return ResponseEntity.ok(list);
    }

    @GetMapping("/received")
    @ApiOperation(value = "查看已领取的vip列表", notes = "")
    public ResponseEntity<List<VipGiftRecord>> received() {
        String userId = SecurityUtils.getUser().getUserId();

        List<VipGiftRecord> list = vipGiftRecordService.list(new LambdaQueryWrapper<>(new VipGiftRecord())
                .eq(VipGiftRecord::getUserId, userId)
                .eq(VipGiftRecord::getStatus, 1)
                .orderByDesc(VipGiftRecord::getCreateTime));
        return ResponseEntity.ok(list);
    }

    @PostMapping("/confirm/{id}")
    @ApiOperation(value = "领取赠送的VIP", notes = "")
    @Transactional
    public ResponseEntity<Void> confirm(@PathVariable String id) {
        String userId = SecurityUtils.getUser().getUserId();
        VipGiftRecord record = vipGiftRecordService.getById(id);
        if(record==null){
            throw new RuntimeException("无效赠送套餐id");
        }

        User user = userService.getById(userId);
        Integer isVip = user.getIsVip();
        Date now = new Date();
//        原来不是vip
        if(isVip==0){
            user.setIsVip(1);
            user.setVipStartTime(now);
            user.setVipEndTime(DateUtil.offsetDay(now,record.getVipDays()));
        }else{
//            原来是vip
            Date vipEndTime = user.getVipEndTime();
            user.setVipEndTime(DateUtil.offsetDay(vipEndTime,record.getVipDays()));
        }

        // TODO: 2021/10/7 添加一个vip开通记录表 ，并添加记录

        //添加每日可解锁次数
        user.setTodayUnlockSurplus(vipPackageService.getDailyFreeUnlockNum());

//        使赠送记录变为已领取
        record.setStatus(1);
        record.setRecvTime(now);
        vipGiftRecordService.updateById(record);

//        更新用户vip状态
        userService.updateById(user);
        return ResponseEntity.ok().build();
    }




}
